package edu.hawaii.senin.iclouds.plotting;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.concurrent.Callable;

import org.jdom.Element;

/**
 * Encapsulates task for plotting routines.
 * 
 * @author Pavel Senin.
 * 
 */
public class GeneralHDOH2OParserTask implements Callable<String> {

  private String resE3FileName;
  private String resE4FileName;
  private String resE5FileName;
  private String resE6FileName;
  private String outFileName;

  private String populationType;
  private List<String> speciesOfInterest;

  /**
   * Instantiates the new task from JDOM Element provided.
   * 
   * @param e the element to parse task from.
   * @param speciesOfInterest the list of species of interest.
   */
  public GeneralHDOH2OParserTask(Element e, List<String> speciesOfInterest) {
    this.outFileName = e.getAttributeValue("output_file");
    this.populationType = e.getAttributeValue("population_type");
    this.resE3FileName = e.getChildText("logfile3e");
    this.resE4FileName = e.getChildText("logfile4e");
    this.resE5FileName = e.getChildText("logfile5e");
    this.resE6FileName = e.getChildText("logfile6e");

    this.speciesOfInterest = speciesOfInterest;
  }

  /**
   * Does the actual work.
   * 
   * @return the dummy debug string to be printed as the status on display.
   * @throws Exception if error encountered.
   */
  public String call() throws Exception {

    GeneralPopulationStorage LogSurfacePopulation3 = null;
    GeneralPopulationStorage LogSurfacePopulation4 = null;
    GeneralPopulationStorage LogSurfacePopulation5 = null;
    GeneralPopulationStorage LogSurfacePopulation6 = null;

    // parse configs
    if ("active".equalsIgnoreCase(this.populationType)) {
      GeneralLogParser logParser = new GeneralLogParser();
      logParser.parse(resE3FileName);
      LogSurfacePopulation3 = new GeneralPopulationStorage(logParser.getActivePopulation());
      logParser.parse(resE4FileName);
      LogSurfacePopulation4 = new GeneralPopulationStorage(logParser.getActivePopulation());
      logParser.parse(resE5FileName);
      LogSurfacePopulation5 = new GeneralPopulationStorage(logParser.getActivePopulation());
      logParser.parse(resE6FileName);
      LogSurfacePopulation6 = new GeneralPopulationStorage(logParser.getActivePopulation());
    }
    else if ("buried".equalsIgnoreCase(this.populationType)) {
      GeneralLogParser logParser = new GeneralLogParser();
      logParser.parse(resE3FileName);
      LogSurfacePopulation3 = new GeneralPopulationStorage(logParser.getBuriedPopulation());
      logParser.parse(resE4FileName);
      LogSurfacePopulation4 = new GeneralPopulationStorage(logParser.getBuriedPopulation());
      logParser.parse(resE5FileName);
      LogSurfacePopulation5 = new GeneralPopulationStorage(logParser.getBuriedPopulation());
      logParser.parse(resE6FileName);
      LogSurfacePopulation6 = new GeneralPopulationStorage(logParser.getBuriedPopulation());
    }
    else if ("total".equalsIgnoreCase(this.populationType)) {
      GeneralLogParser logParser = new GeneralLogParser();
      logParser.parse(resE3FileName);
      LogSurfacePopulation3 = new GeneralPopulationStorage(logParser.getTotalPopulation());
      logParser.parse(resE4FileName);
      LogSurfacePopulation4 = new GeneralPopulationStorage(logParser.getTotalPopulation());
      logParser.parse(resE5FileName);
      LogSurfacePopulation5 = new GeneralPopulationStorage(logParser.getTotalPopulation());
      logParser.parse(resE6FileName);
      LogSurfacePopulation6 = new GeneralPopulationStorage(logParser.getTotalPopulation());

    }

    BufferedWriter writer = new BufferedWriter(new FileWriter(new File(this.outFileName)));

    for (String s : speciesOfInterest) {
      writer.write(s + ",");
      writer.write(LogSurfacePopulation3.getPercentage(s) + ",");
      writer.write(LogSurfacePopulation4.getPercentage(s) + ",");
      writer.write(LogSurfacePopulation5.getPercentage(s) + ",");
      writer.write(LogSurfacePopulation6.getPercentage(s) + "\n");
    }

    writer.flush();
    writer.close();

    return "Dummy debug: " + this.outFileName + " done!";

  }

}
